{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 2\n",
      "1 7\n",
      "[0, 1]\n"
     ]
    }
   ],
   "source": [
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def twoSum(self, nums: List[int], target: int) -> List[int]:\n",
    "        # 定义哈希表\n",
    "        hashtable = dict()\n",
    "        # 遍历数组，查找哈希表中是否存在 target - nums[i] 的值\n",
    "        for i, num in enumerate(nums):\n",
    "            print(i, num)\n",
    "            if target - num in hashtable:\n",
    "                return [hashtable[target - num], i]\n",
    "            # 将数组元素作为键，数组下标作为值存入哈希表\n",
    "            hashtable[num] = i\n",
    "        return []\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    nums = [2, 7, 11, 15]\n",
    "    target = 9\n",
    "    print(Solution().twoSum(nums, target))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pylearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
